package com.beidouin.GnssMain.sample;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.widget.Toast;

import com.beidouin.GnssMain.EventSerialReceive;
import com.beidouin.GnssMain.fragment.GpsListFragment;
import com.beidouin.GnssMain.fragment.GpsSkyFragment;
import com.beidouin.GnssMain.models.BeidouStatus;
import com.beidouin.GnssMain.models.PilotInfo;
import com.beidouin.GnssMain.R;


/**
 * Created by yuyang on 16/6/23.
 */
public class SatelliteActivity extends SerialPortActivity {
    protected final String TAG = this.getClass().getName() + "@" + this.hashCode();
    static GpsSkyFragment sSkyFragment;
    static GpsListFragment sListFragment;
    MyPagerAdapter adapterViewPager;

    private int count = 0;

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
//        ActionBar actionBar = getSupportActionBar();
        setContentView(R.layout.activity_satellite_layout);
        initBottomView();
//        skyFragment = new GpsSkyFragment();
//
//        getSupportFragmentManager().beginTransaction()
//                .add(R.id.container, skyFragment).commit();

        activityType = 1;

        ViewPager vpPager = (ViewPager) findViewById(R.id.vpPager);
        adapterViewPager = new MyPagerAdapter(getSupportFragmentManager());
        vpPager.setAdapter(adapterViewPager);
        satelliteImage.setImageResource(R.drawable.satellite_selected);
        satelliteText.setTextColor(getResources().getColor(R.color.app_main));

        adapterViewPager.notifyDataSetChanged();
    }

    public static class MyPagerAdapter extends FragmentPagerAdapter {
        private static int NUM_ITEMS = 2;

        public MyPagerAdapter(FragmentManager fragmentManager) {
            super(fragmentManager);
        }

        // Returns total number of pages
        @Override
        public int getCount() {
            return NUM_ITEMS;
        }

        // Returns the fragment to display for that page
        @Override
        public Fragment getItem(int position) {
            switch (position) {
                case 0: // Fragment # 0 - This will show FirstFragment
                    sSkyFragment = GpsSkyFragment.newInstance(0, "Page # 1");
                    return sSkyFragment;
                case 1:
                    sListFragment = GpsListFragment.newInstance(1, "Page # 2");
                    return sListFragment;
                default:
                    return null;
            }
        }

        // Returns the page title for the top indicator
        @Override
        public CharSequence getPageTitle(int position) {
            if (position == 0) {
                return "卫星天顶图";
            } else {
                return "卫星信息列表";
            }

        }

    }

    private void validateUI() {
        BeidouStatus.getInstance().setData(PilotInfo.getBeidouSatelliteHashMap());
        sSkyFragment.validateUI();
        sListFragment.validateUI();

    }

    @Override
    protected Activity getCurrentActivity() {
        return SatelliteActivity.this;
    }

    public void onEventMainThread(final EventSerialReceive event) {
        runOnUiThread(new Runnable() {
            public void run() {
                if (event.eventType == EventSerialReceive.EVENT_UPDATE_ACTIVITY) { //data from serial port
                    if ((event.iData & EventSerialReceive.UPDATE_SATELLITE) != 0) {
                        validateUI();
                    }
                }
            }
        });
    }
}
